
package com.example.testutil.util.contentprovider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;

import com.example.testutil.util.LogUtil;
import com.example.testutil.util.sql.DbOperate;

public class MyContentProvider extends ContentProvider {
    private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);

    private static final int PERSONS = 1;

    private static final int PERSON_ID = 2;

    private DbOperate dbOperate;
    static {
        matcher.addURI(Persons.AUTHORITY, "person", PERSONS);
        matcher.addURI(Persons.AUTHORITY, "person/#", PERSON_ID);
    }

    @Override
    public boolean onCreate() {
        LogUtil.d("--------------oncreate");
        dbOperate = DbOperate.getInstance(getContext());
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        LogUtil.d("------------------------------------");
        switch (matcher.match(uri)) {
            case PERSONS:
                return dbOperate.query("raymond", projection, selection, selectionArgs, null, null, sortOrder);
            case PERSON_ID:
                long id = ContentUris.parseId(uri);
                String where = Persons.Person._ID + "=" + id;
                if (selection != null && !selection.equals("")) {
                    where = where + "and" + selection;
                }
                return dbOperate.query("raymond", projection, where, selectionArgs, null, null, sortOrder);
            default:
                throw new IllegalArgumentException("未知Uri:" + uri);
        }
    }

    @Override
    public String getType(Uri uri) {
        LogUtil.d("-----------------------------------------------");
        switch (matcher.match(uri)) {
            case PERSONS:
                return "vnd.android.cursor.dir/org.raymond.dictprovider";
            case PERSON_ID:
                return "vnd.android.cursor.item/org.raymond.dictprovider";

            default:
                throw new IllegalArgumentException("未知Uri:" + uri);
        }
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        long rowId = dbOperate.insert("raymond", Persons.Person._ID, values);
        if (rowId > 0) {
            Uri personUri = ContentUris.withAppendedId(uri, rowId);
            getContext().getContentResolver().notifyChange(personUri, null);
            return personUri;
        }
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int num = 0;
        switch (matcher.match(uri)) {
            case PERSONS:
                num = dbOperate.delete("raymond", selection, selectionArgs);
                break;
            case PERSON_ID:
                long id = ContentUris.parseId(uri);
                String where = Persons.Person._ID + "=" + id;
                if (selection != null && !selection.equals("")) {
                    where = where + "and" + selection;
                }
                num = dbOperate.delete("raymond", where, selectionArgs);
                return num;
            default:
                throw new IllegalArgumentException("未知Uri:" + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        int num = 0;
        switch (matcher.match(uri)) {
            case PERSONS:
                num = dbOperate.update("raymond", values, selection, selectionArgs);
                break;
            case PERSON_ID:
                long id = ContentUris.parseId(uri);
                String where = Persons.Person._ID + "=" + id;
                if (selection != null && !selection.equals("")) {
                    where = where + "and" + selection;
                }
                num = dbOperate.delete("raymond", where, selectionArgs);
                return num;
            default:
                throw new IllegalArgumentException("未知Uri:" + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return 0;

    }

}
